package com.zwh.autoconfigure.interceptor;

import java.util.List;

import javax.servlet.Filter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



/**
 * @author zwh
 * @date 2020/12/9 14:48
 **/
@Configuration
@ConditionalOnProperty(name="web.interceptor.sign.enabled", havingValue="true", matchIfMissing=false)
public class CommonSignValidFilterConfig {

    @Autowired
    CommonSignValidFilterProperties commonSignValidFilterProperties;

    @Bean
    public Filter commonSignValidFilter(){
        return new CommonSignValidFilter();
    }


    @Bean
    public FilterRegistrationBean setCommonSignValidFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(commonSignValidFilter());

        List<String> apps = commonSignValidFilterProperties.getAddPathPatterns();
        List<String> epps = commonSignValidFilterProperties.getExcludePathPatterns();
        apps.forEach(item->{
            if(StringUtils.isNotBlank(item)) {
                filterRegistrationBean.addUrlPatterns(item);
            }
        });
        String excludeUrlPatterns = StringUtils.join(epps,",");
        filterRegistrationBean.addInitParameter("exclusions",
                excludeUrlPatterns);
        filterRegistrationBean.setOrder(3);   //order的数值越小，在所有的filter中优先级越高
        return filterRegistrationBean;
    }
}
